package com.erp.erp_ui.InspectCargo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.erp_entitys.CustomerComplaint.CustomerComplaintAndProduct;
import com.erp.erp_entitys.CustomerComplaint.CustomerComplaintVo;
import com.erp.erp_entitys.InspectCargo.*;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Ftp.IFtpService;
import com.erp.erp_servers.InspectCargo.IInspectCargoFileService;
import com.erp.erp_servers.InspectCargo.IInspectCargoProductService;
import com.erp.erp_servers.InspectCargo.IInspectCargoService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.DateUtils;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping(value = "inspectCargo")
@Api( tags = "验货商检")
public class InspectCargoControl extends Base {
    @Autowired
    private IInspectCargoService inspectCargoService;
    @Autowired
    private IInspectCargoProductService inspectCargoProductService;
    @Autowired
    private IInspectCargoFileService inspectCargoFileService;
    @Autowired
    private IFtpService ftpService;

    /**
     * 新增验货商检
     * @param inspectCargo
     * @return
     */
    @RequestMapping(value = "insert" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增验货商检", notes = "新增验货商检", httpMethod = "POST")
    public R<Integer> insert(@RequestBody InspectCargoEntity inspectCargo){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                if(StringUtils.isEmpty(inspectCargo.getOddNumber())){
                    return  R.buildError(ResultStatusEnum.NOT_EMPTY);
                }
                boolean isExist = inspectCargoService.isExist(inspectCargo.getOddNumber());
                if(isExist){
                    return  R.buildError(ResultStatusEnum.BILL_NO_REPEAT);
                }
                UserEntity user = (UserEntity )cacheUser;
                inspectCargo.setExecutor(user.getCode());
                inspectCargo.setCreateTime(new Date());
                inspectCargo.setBoxStatus("1");

                int num = inspectCargoService.insertProAndFile(inspectCargo);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询验货商检
     * @param id
     * @return
     */
    @RequestMapping(value = "selectByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询验货商检",notes = "按id查询验货商检",httpMethod = "POST")
    public R<InspectCargoEntity> selectByKey(@PathVariable("id") Integer id){
        try {
            if (id==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            InspectCargoEntity inspectCargo = inspectCargoService.selectByPrimaryKey(id);
            if (inspectCargo==null){
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(inspectCargo);
        } catch (NumberFormatException e1){
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
        } catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新验货商检
     * 部分字段
     * @param inspectCargo
     * @return
     */
    @RequestMapping(value = "update")
    @ResponseBody
    @ApiOperation(value = "按id更新验货商检", notes = "按id更新验货商检", httpMethod = "POST")
    public R<Integer> update(@RequestBody InspectCargoEntity inspectCargo){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                inspectCargo.setExecutor(user.getCode());
                int num = inspectCargoService.updateInfoById(inspectCargo);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除验货商检-备注
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "delete/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除验货商检", notes = "按id删除验货商检", httpMethod = "POST")
    public R<Integer> delete(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                InspectCargoEntity inspectCargo = inspectCargoService.selectByPrimaryKey(id);
                int num = 0;
                if(inspectCargo!=null){
                    num = inspectCargoService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增验货商检产品记录
     * @param inspectCargoProduct
     * @return
     */
    @RequestMapping(value = "insertProduct" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增验货商检产品", notes = "新增验货商检产品", httpMethod = "POST")
    public R<Integer> insertProduct(@RequestBody InspectCargoProductEntity inspectCargoProduct){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = inspectCargoProductService.insertSelective(inspectCargoProduct);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (DataIntegrityViolationException e1){
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.DATA_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询验货商检产品记录
     * @param id
     * @return
     */
    @RequestMapping(value = "selectProductByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询验货商检产品",notes = "按id查询验货商检产品",httpMethod = "POST")
    public R<InspectCargoProductEntity> selectProductByKey(@PathVariable("id") Integer id){
        try {
            if (id==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            InspectCargoProductEntity inspectCargoProduct = inspectCargoProductService.selectByPrimaryKey(id);
            if (inspectCargoProduct==null){
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(inspectCargoProduct);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新验货商检产品记录
     * 部分字段
     * @param inspectCargoProduct
     * @return
     */
    @RequestMapping(value = "updateProduct")
    @ResponseBody
    @ApiOperation(value = "按id更新验货商检产品", notes = "按id更新验货商检产品", httpMethod = "POST")
    public R<Integer> updateProduct(@RequestBody InspectCargoProductEntity inspectCargoProduct){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = inspectCargoProductService.updateByPrimaryKeySelective(inspectCargoProduct);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除验货商检-备注
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteProduct/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除验货商检产品", notes = "按id删除验货商检产品", httpMethod = "POST")
    public R<Integer> deleteProduct(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                InspectCargoProductEntity inspectCargoProduct = inspectCargoProductService.selectByPrimaryKey(id);
                int num = 0;
                if(inspectCargoProduct!=null){
                    num = inspectCargoProductService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 新增验货商检产品记录
     * @param inspectCargoFile
     * @return
     */
    @RequestMapping(value = "insertFile" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增验货商检附件", notes = "新增验货商检附件", httpMethod = "POST")
    public R<Integer> insertFile(@RequestBody InspectCargoFileEntity inspectCargoFile){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = inspectCargoFileService.insertSelective(inspectCargoFile);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (DataIntegrityViolationException e1){
            e1.printStackTrace();
            return R.buildError(ResultStatusEnum.DATA_ERROR);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id查询验货商检产品记录
     * @param id
     * @return
     */
    @RequestMapping(value = "selectFileByKey/{id}")
    @ResponseBody
    @ApiOperation(value = "按id查询验货商检附件",notes = "按id查询验货商检附件",httpMethod = "POST")
    public R<InspectCargoFileEntity> selectFileByKey(@PathVariable("id") Integer id){
        try {
            if (id==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            InspectCargoFileEntity inspectCargoFile = inspectCargoFileService.selectByPrimaryKey(id);
            if (inspectCargoFile==null){
                return R.buildError(ResultStatusEnum.NOT_FOUND);
            }
            return R.build(inspectCargoFile);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新验货商检产品记录
     * 部分字段
     * @param inspectCargoFile
     * @return
     */
    @RequestMapping(value = "updateFile")
    @ResponseBody
    @ApiOperation(value = "按id更新验货商检", notes = "按id更新验货商检产品记录", httpMethod = "POST")
    public R<Integer> updateFile(@RequestBody InspectCargoFileEntity inspectCargoFile){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = inspectCargoFileService.updateByPrimaryKeySelective(inspectCargoFile);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按主键id删除验货商检-附件
     * @param id
     * @return 操作记录数
     */
    @RequestMapping(value = "deleteFile/{id}")
    @ResponseBody
    @ApiOperation(value = "按id删除验货商检附件", notes = "按id删除验货商检附件", httpMethod = "POST")
    public R<Integer> deleteFile(@PathVariable("id") Integer id){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                UserEntity user = (UserEntity )cacheUser;
                InspectCargoFileEntity inspectCargoFile = inspectCargoFileService.selectByPrimaryKey(id);
                int num = 0;
                if(inspectCargoFile!=null){
                    num = inspectCargoFileService.deleteByPrimaryKey(id);
                }
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 根据icId查询验货商检 -产品
     * @param
     * @return
     */
    @RequestMapping(value = "selectProductByIcId/{icId}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据icId查询验货商检产品记录", notes ="根据icId查询验货商检产品记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="icId",value = "验货商检id"),
    })
    public R<List<InspectCargoProductEntity>> selectProductByIcId(@PathVariable("icId") Integer icId){
        try {
            if (icId==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            List<InspectCargoProductEntity> list = inspectCargoProductService.selectProductByIcId(icId);
            if (list==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }

    /**
     * 根据icId查询验货商检 -附件
     * @param
     * @return
     */
    @RequestMapping(value = "selectFileByIcId/{icId}",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据icId查询验货商检附件记录", notes ="根据icId查询验货商检附件记录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="icId",value = "验货商检id"),
    })
    public R<List<InspectCargoFileEntity>> selectFileByIcId(@PathVariable("icId") Integer icId){
        try {
            if (icId==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            List<InspectCargoFileEntity> list = inspectCargoFileService.selectFileByIcId(icId);
            if (list==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }


    /**
     * 根据给定的条件查询验货商检
     * @param
     * @return
     */
    @RequestMapping(value = "selectInspectCargoList",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据条件查询验货商检", notes ="根据条件查询验货商检", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="boxStatus",value = "单据类型"),
            @ApiImplicitParam(name="numberLike",value = "单据信息"),
            @ApiImplicitParam(name="executor",value = "所属操作员"),
            @ApiImplicitParam(name="salesman",value = "所属业务员"),
            @ApiImplicitParam(name="startInspectTime",value = "验货开始日期"),
            @ApiImplicitParam(name="endInspectTime",value = "验货结束日期"),
            @ApiImplicitParam(name="startCreateTime",value = "创建开始时间"),
            @ApiImplicitParam(name="endCreateTime",value = "创建结束时间"),
            @ApiImplicitParam(name="pageNum",value = "当前页"),
            @ApiImplicitParam(name="pageSize",value = "页内条数")
    })
    public R selectInspectCargoList(@RequestBody InspectCargoVo icVo){
        try {
            if (icVo==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            if (icVo.getStartInspectTime()!=null){
                Date startDate = DateUtils.getStartOfDay(icVo.getStartInspectTime());
                icVo.setStartInspectTime(startDate);
                if (icVo.getEndInspectTime()==null){
                    icVo.setEndInspectTime(new Date());
                }
                Date endDate = DateUtils.getEndOfDay(icVo.getEndInspectTime());
                icVo.setEndInspectTime(endDate);
            }
            if (icVo.getStartCreateTime()!=null){
                Date startDate = DateUtils.getStartOfDay(icVo.getStartCreateTime());
                icVo.setStartCreateTime(startDate);
                if (icVo.getEndCreateTime()==null){
                    icVo.setEndCreateTime(new Date());
                }
                Date endDate = DateUtils.getEndOfDay(icVo.getEndCreateTime());
                icVo.setEndCreateTime(endDate);
            }
            PageHelper.startPage(icVo.getPageNum(),icVo.getPageSize());
            PageInfo<InspectCargoEntity> pageInfo = inspectCargoService.selectInspectCargoList(icVo);
            if(pageInfo.getList()==null || pageInfo.getTotal()==0){
                return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按id更新客诉审核状态
     * 部分字段
     * @param icVo
     * @return 操作记录数
     */
    @RequestMapping(value = "updateBoxStatusBykey")
    @ResponseBody
    @ApiOperation(value = "按id更新客诉审核状态", notes = "按id更新客诉审核状态", httpMethod = "POST")
    public R<Integer> updateBoxStatusBykey(@RequestBody InspectCargoVo icVo){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                if(null==icVo || StringUtils.isEmpty(icVo.getBoxStatus())){
                    return  R.buildError(ResultStatusEnum.PARAMETER_ERROR);
                }
                int num = inspectCargoService.updateBoxStatusBykey(icVo);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 分页查询 出运计划列表
     * @param carryPlanVo 查询参数
     * @return
     */
    @RequestMapping(value = "selectCarryPlanByPage",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "按条件查询出运计划", notes ="按条件查询出运计划", httpMethod = "GET")
    public R selectCarryPlanByPage(CarryPlanVo carryPlanVo){
        try {
            if (carryPlanVo==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<CarryPlanVo> pageInfo = inspectCargoProductService.selectCarryPlanList(carryPlanVo);
            if (pageInfo==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按carryPlanId查询出运计划对应-产品列表
     * @param carryPlanId
     * @return
     */
    @RequestMapping(value = "selectCarryPlanProductByCpId",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "按carryPlanId查询出运计划对应-产品列表", notes ="按carryPlanId查询出运计划对应-产品列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="carryPlanId",value = "出运计划id"),
    })
    public R selectCarryPlanProductByCpId(Integer carryPlanId){
        try {
            if(null == carryPlanId){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<InspectCargoProductEntity> pageInfo = inspectCargoProductService.selectCarryPlanProductByCpId(carryPlanId);
            if (pageInfo==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按carryPlanIds查询出运计划对应-产品列表
     * @param
     * @return
     */
    @RequestMapping(value = "selectCarryPlanProductByCarryPlanIds",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按carryPlanIds查询出运计划对应-产品列表", notes ="按carryPlanIds查询出运计划对应-产品列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="carryPlanIds",value = "出运计划ids"),
    })
    public R selectCarryPlanProductByCarryPlanIds(@RequestBody Integer[] carryPlanIds){
        try {
            if(null == carryPlanIds || carryPlanIds.length==0){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<InspectCargoProductEntity> pageInfo = inspectCargoProductService.selectCarryPlanProductByCarryPlanIds(Arrays.asList(carryPlanIds));
            if (pageInfo==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 分页查询 采购单据列表
     * @param stockVo 查询参数
     * @return
     */
    @RequestMapping(value = "selectStockByPage",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "按条件查询出运计划", notes ="按条件查询出运计划", httpMethod = "GET")
    public R selectStockByPage(StockVo stockVo){
        try {
            if (stockVo==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<StockVo> pageInfo = inspectCargoProductService.selectStockList(stockVo);
            if (pageInfo==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按采购单据id查询出运计划对应-产品列表
     * @param stockId
     * @return
     */
    @RequestMapping(value = "selectStockProductByStockId",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "按carryPlanId查询出运计划对应-产品列表", notes ="按carryPlanId查询出运计划对应-产品列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name="carryPlanId",value = "出运计划id"),
    })
    public R selectStockProductByStockId(Integer stockId){
        try {
            if(null == stockId){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<InspectCargoProductEntity> pageInfo = inspectCargoProductService.selectStockProductByStockId(stockId);
            if (pageInfo==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按carryPlanIds查询出运计划对应-产品列表
     * @param
     * @return
     */
    @RequestMapping(value = "selectStockProductByStockIds",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按carryPlanIds查询出运计划对应-产品列表", notes ="按carryPlanIds查询出运计划对应-产品列表", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name="carryPlanIds",value = "出运计划ids"),
    })
    public R selectStockProductByStockIds(@RequestBody Integer[] stockIds){
        try {
            if(null == stockIds || stockIds.length==0){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            PageInfo<InspectCargoProductEntity> pageInfo = inspectCargoProductService.selectStockProductByStockIds(Arrays.asList(stockIds));
            if (pageInfo==null){
                return R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 查询最新单号  按月累计
     * @return 单号
     */
    @RequestMapping(value = "getLatestOddNumber")
    @ResponseBody
    @ApiOperation(value = "查询最新单号",notes = "查询最新单号",httpMethod = "POST")
    public R<String> selectByKey(){
        try {
            Integer latestOddNumberCount = Integer.parseInt(inspectCargoService.getLatestOddNumber());
            int digit = 3;//位数
            int max = Integer.parseInt("1"+String.format("%1$0"+digit+"d",0));//右补0
            while (latestOddNumberCount>=max){
                digit++;
                max = Integer.parseInt("1"+String.format("%1$0"+digit+"d",0));
            }
            String latestOddNumber = "";
            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String nowDateStr = sdf.format(d);
            String suffix = String.format("%0"+digit+"d", (latestOddNumberCount+1));//左补齐0
            latestOddNumber = "YH"+nowDateStr+suffix;
            return R.build(latestOddNumber);
        } catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 按出运计划单号 生成验货商检
     * @param loadPlanNo
     * @return
     */
    @RequestMapping(value = "generateInspectCargoByShipmentNo" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "按出运计划单号 生成验货商检", notes = "按出运计划单号 生成验货商检", httpMethod = "POST")
    public R<Integer> generateInspectCargoByShipmentNo(String loadPlanNo){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                int num = inspectCargoService.generateInspectCargoByShipmentNo(loadPlanNo);
                boolean isExist = inspectCargoService.selectInspectCargoListByShipmentNo(loadPlanNo);
                return R.build(num);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 统计  按条件查询客诉
     * @param 条件
     * @return 验货及对应产品
     */
    @RequestMapping(value = "selectMainAndProductList",method = RequestMethod.POST)
    @ResponseBody
    public R selectMainAndProductList(@RequestBody InspectCargoVo vo){
        try {
            if (vo==null){
                return R.buildError(ResultStatusEnum.PARAMETER_ERROR);
            }
            if (vo.getStartInspectTime()!=null){
                Date startDate = DateUtils.getStartOfDay(vo.getStartInspectTime());
                vo.setStartInspectTime(startDate);
                if (vo.getEndInspectTime()==null){
                    vo.setEndInspectTime(new Date());
                }
                Date endDate = DateUtils.getEndOfDay(vo.getEndInspectTime());
                vo.setEndInspectTime(endDate);
            }else{
                Map<String, Date> map = DateUtils.getNowMonthNew();
                Date startDate = map.get("startDate");
                Date endDate = map.get("endDate");
                vo.setStartInspectTime(startDate);
                vo.setEndInspectTime(endDate);
            }
            PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
            List<InspectCargoAndProduct> list = inspectCargoService.selectInspectCargoAndProductList(vo);
            //获取分页信息
            PageInfo pageInfo = new PageInfo(list);
            if(CollectionUtils.isEmpty(list)){
                pageInfo.setList(null);
                return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
            }else{
                pageInfo.setList(list);
            }
            if(pageInfo.getTotal()==0){
                if(pageInfo.getList()==null || pageInfo.getTotal()==0){
                    return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
                }
            }
            return R.build(0,"", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 统计-导出Excel
     * @return 统计信息
     */
    @GetMapping(value = "export/selectMainAndProductList")
    @ResponseBody
    public R exportMainAndProductList(InspectCargoVo vo){
        try {
            JSONObject obj = new JSONObject();
            JSONArray data = new JSONArray();
            JSONArray titleList = getTitleStatistics();//标题行
            data.add(titleList);
            List<InspectCargoAndProduct> list = inspectCargoService.selectInspectCargoAndProductList(vo);
            if(!CollectionUtils.isEmpty(list)){
                BigDecimal amountTotal = BigDecimal.ZERO;//数量-总和
                for (InspectCargoAndProduct icp : list){
                    JSONArray infoList = new JSONArray();
                    infoList.add(StringUtils.isNotEmpty(icp.getOddNumber())?icp.getOddNumber():"");
                    infoList.add(DateUtils.format(icp.getInspectTime()));
                    infoList.add(StringUtils.isNotEmpty(icp.getCompany())?icp.getCompany():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getComplaintUnit())?icp.getComplaintUnit():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getInspectType())?icp.getInspectType():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getExecutor())?icp.getExecutor():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getInspector())?icp.getInspector():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getSalesContractNo())?icp.getSalesContractNo():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getLoadPlanNo())?icp.getLoadPlanNo():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getPurchaseOrderNo())?icp.getPurchaseOrderNo():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getProductNo())?icp.getProductNo():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getSpecNo())?icp.getSpecNo():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getNormExplain())?icp.getNormExplain():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getChineseName())?icp.getChineseName():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getEnglishName())?icp.getEnglishName():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getChineseDesc())?icp.getChineseDesc():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getEnglishDesc())?icp.getEnglishDesc():"");
                    infoList.add(StringUtils.isNotEmpty(icp.getColour())?icp.getColour():"");
                    BigDecimal amount = icp.getAmount()!=null?icp.getAmount():BigDecimal.ZERO;
                    amountTotal = amountTotal.add(amount);
                    infoList.add(amount);
                    infoList.add(StringUtils.isNotEmpty(icp.getRemark())?icp.getRemark():"");
                    data.add(infoList);
                }
                //合计行
                JSONArray totalList = new JSONArray();
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add("");
                titleList.add(amountTotal);
                titleList.add("");
                data.add(totalList);
            }
            obj.put("data", data);
            return R.build(obj);
        }catch (Exception e){
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取excel的title信息
     * @return excel的title信息
     */
    private JSONArray getTitleStatistics(){
        JSONArray titleList = new JSONArray();
        titleList.add("验货单号");
        titleList.add("验货日期");
        titleList.add("验货公司");
        titleList.add("制单公司");//3
        titleList.add("验货类型");//4
        titleList.add("操作员");//5
        titleList.add("业务员");//6
        titleList.add("销售合同号");
        titleList.add("出运计划单号");
        titleList.add("采购单号");
        titleList.add("产品编码");
        titleList.add("规格编码");
        titleList.add("规格说明");
        titleList.add("中文名称");
        titleList.add("英文名称");
        titleList.add("中文描述");
        titleList.add("英文描述");
        titleList.add("颜色");//17
        titleList.add("数量");
        titleList.add("备注");

        return titleList;
    }
}
